/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package racesuitefx;

import DAO.RaceDAO;
import DAO.RaceResult;
import DAO.StarterDAO;
import DAO.TagDAO;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import racesuitefx.entities.Race;
import racesuitefx.entities.Starter;
import racesuitefx.entities.StarterPK;
import racesuitefx.entities.Tag;

/**
 * FXML Controller class
 *
 * @author Wolfgang
 */
public class FXMLCreateStarterController implements Initializable {

    /**
     * Initializes the controller class.
     */
    Race race;
    StarterDAO starterDAO = new StarterDAO();
    TagDAO tagDAO = new TagDAO();
    RaceDAO raceDAO = new RaceDAO();
     
    private ObservableList<Starter> data;
    @FXML
    private TableView<Starter> table;
    @FXML
    private TextField raceName;
    
    @FXML
    private TextField firstName;
    
    @FXML
    private TextField lastName;
    
    @FXML
    private TextField startNumber;
    
    @FXML
    private TextField tagId;
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        data = FXCollections.observableArrayList();  
        TableColumn firstName = new TableColumn("Vorname");
        firstName.setCellValueFactory(new PropertyValueFactory<Starter, String>("firstName"));

        TableColumn lastName = new TableColumn("Nachname");
        lastName.setCellValueFactory(new PropertyValueFactory<Starter, String>("lastName"));

        TableColumn startNumber = new TableColumn("StartNummer");
        startNumber.setCellValueFactory(new PropertyValueFactory<Starter, String>("startNumber"));
        table.setItems(data);
        table.getColumns().addAll(firstName, lastName, startNumber);
        table.autosize();
    }
    
    @FXML
    private void scan(ActionEvent event) {
        this.tagId.setText(Util.CommunicateUtil.readTagId());
    }
    @FXML
    private void add(ActionEvent event) {
        Starter s = new Starter();
        s.setFirstName(this.firstName.getText());
        s.setLastName(this.lastName.getText());
        s.setStartNumber(Integer.parseInt(this.startNumber.getText()));
        Tag tag = (Tag) tagDAO.find(this.tagId.getText());
        s.setTag(tag);
        StarterPK pk = new StarterPK(this.race.getRaceId(), tag.getTagId());
        s.setStarterPK(pk);
        starterDAO.create(s);
        this.setRace(this.raceDAO.find(this.race.getRaceId()));
        this.firstName.setText("");
        this.lastName.setText("");
        this.tagId.setText("");
    }
    public void setRace(Race r){
        this.race = r;
        this.raceName.setText(this.race.getName());
        this.startNumber.setText(""+(this.race.getStarterCollection().size()+1));
        ObservableList<Starter> datas = FXCollections.observableArrayList(r.getStarterCollection());
        table.setItems(datas);
    }
    public Race getRace(){
        return this.race;
    }
    
}
